from command import *
from typing import List


class RemoteControlWithUndo:
    def __init__(self):
        self._on_commands: List[Command] = []
        self._off_commands: List[Command] = []
        self._command_history: List[Command] = []

        for i in range(7):
            self._on_commands.append(NoCommand())
            self._off_commands.append(NoCommand())

    def set_command(self, slot: int, on_command: Command, off_command: Command):
        self._on_commands[slot] = on_command
        self._off_commands[slot] = off_command

    def on_button_was_pushed(self, slot: int):
        self._on_commands[slot].execute()
        self._command_history.append(self._on_commands[slot])

    def off_button_was_pushed(self, slot: int):
        self._off_commands[slot].execute()
        self._command_history.append(self._off_commands[slot])

    def undo_button_was_pushed(self):
        if self._command_history:
            self._command_history.pop().undo()

    def __str__(self):
        result = ["\n----- Remote Control -----\n"]
        for i in range(len(self._on_commands)):
            result.append(f"[slot {i}] {type(self._on_commands[i])}    {type(self._off_commands[i])}\n")
        result.append(f"[undo] {self._command_history}")
        return "".join(result)


if __name__ == '__main__':
    from light import *
    remote_control = RemoteControlWithUndo()

    living_room_light = Light("Living Room")
    living_room_light_on = LightOnCommand(living_room_light)
    living_room_light_off = LightOffCommand(living_room_light)

    remote_control.set_command(0, living_room_light_on, living_room_light_off)

    remote_control.on_button_was_pushed(0)
    remote_control.off_button_was_pushed(0)
    print(remote_control)
    remote_control.undo_button_was_pushed()
    remote_control.off_button_was_pushed(0)
    remote_control.on_button_was_pushed(0)
    print(remote_control)
    remote_control.undo_button_was_pushed()

    from ceiling_fan import *

    ceiling_fan = CeilingFan("Living Room")
    ceiling_fan_medium = CeilingFanMediumCommand(ceiling_fan)
    ceiling_fan_high = CeilingFanHighCommand(ceiling_fan)
    ceiling_fan_off = CeilingFanOffCommand(ceiling_fan)

    remote_control.set_command(0, ceiling_fan_medium,ceiling_fan_off)
    remote_control.set_command(1, ceiling_fan_high, ceiling_fan_off)

    remote_control.on_button_was_pushed(0)
    remote_control.off_button_was_pushed(0)
    print(remote_control)
    remote_control.undo_button_was_pushed()

    remote_control.on_button_was_pushed(1)
    print(remote_control)
    remote_control.undo_button_was_pushed()